﻿
namespace Sai.Test.Rules
{
    using Sai.Core;
    using Sai.Core.Rules;
    using Sai.Test.TestDataBuilders;
    using Sai.Test.XUnit;
    using Xunit;

    public class CheckTypesObsoleteFacts
    {
        [Fact, TouchingFileSystemTest]
        public void Both_Versions_Of_Class_Are_Obsolete_Is_Not_Flagged()
        {
            var oldClassBuilder = CompilableClassBuilder.PublicClass()
                .Named("Account")
                .IsObsolete("Do not use");

            string oldCode = BoilerPlateCodeBuilder.BuildFor(oldClassBuilder);

            var newClassBuilder = CompilableClassBuilder.PublicClass()
                .Named("Account")
                .IsObsolete("Do not use");

            string newCode = BoilerPlateCodeBuilder.BuildFor(newClassBuilder);

            AssemblyDifference diff = AssemblyDifferenceBuilder.Differences()
                                            .CodeWas(oldCode)
                                            .CodeIsNow(newCode)
                                            .CheckedUsing(new CheckTypesObsolete())
                                            .Build();

            Assert.False(diff.HasAnyChanges);

            Assert.Null(diff.AddedTypes);
            Assert.Null(diff.RemovedTypes);
            Assert.Null(diff.ChangedTypes);
        }

        [Fact, TouchingFileSystemTest]
        public void Later_Version_Of_Class_Is_Obsolete_Is_Flagged()
        {
            var oldClassBuilder = CompilableClassBuilder.PublicClass()
                .Named("Account");

            string oldCode = BoilerPlateCodeBuilder.BuildFor(oldClassBuilder);

            var newClassBuilder = CompilableClassBuilder.PublicClass()
                .Named("Account")
                .IsObsolete("Do not use");

            string newCode = BoilerPlateCodeBuilder.BuildFor(newClassBuilder);

            AssemblyDifference diff = AssemblyDifferenceBuilder.Differences()
                .CodeWas(oldCode)
                .CodeIsNow(newCode)
                .CheckedUsing(new CheckTypesObsolete())
                .Build();

            Assert.True(diff.HasAnyChanges);

            Assert.Null(diff.AddedTypes);
            Assert.Null(diff.RemovedTypes);
            Assert.Null(diff.ChangedTypes);

            Assert.Equal(1, diff.ObsoleteTypes.Count);
        }

    }
}
